<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章分类</span>
        <el-button
          style="float: right"
          type="primary"
          @click="openDialog('add')"
          >添加分类</el-button
        >
      </div>

      <el-table :data="cateList" style="width: 100%">
        <el-table-column prop="id" label="序号">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操纵" width="180">
          <template #default="{ row }">
            <div class="actions">
              <el-button type="primary" @click="openDialog('edit', row.id)">修改</el-button>
              <el-button type="danger" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <template #title>
        <i class="el-icon-info" style="color: #4f81ff; font-size: 16px"></i>
        <span style="font-weight: bold; margin-left: 6px">{{ dialogTitle }}</span>
      </template>
      <el-form :model="form" ref="myForm" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="form.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="form.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
  title="提示"
  :visible.sync="delDialog"
  width="30%"
  :before-close="handleClose">
  <template #title>
    <i class="el-icon-info" style="color: #4f81ff; font-size: 16px;"></i>
    <span style="font-weight: bold;margin-left: 6px;">提示</span>
  </template>
  <span>确定要删除本条信息吗？</span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="delDialog = false">取消</el-button>
    <el-button type="primary" @click="delCate">确定删除</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { getCateList, addArticleCate, getArticleCateInfo, updateArticleCate, delArticleCate } from "@/api/article";
export default {
  name: "ArtCate",
  data() {
    return {
      cateList: [],
      dialogType: '',
      id: 0,
      form: {
        cate_name: "",
        cate_alias: "",
      },
      dialogVisible: false,
      delDialog: false,
      rules: {
        cate_name: [{ required: true, message: "请输入面经标题", trigger: "blur" }],
        cate_alias: [
          { required: true, message: "请输入面经内容", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const res = await getCateList();
      console.log(res);
      this.cateList = res.data
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    async submit() {
      try {
        if (this.dialogType === 'add') {
            // 校检表单
            await this.$refs.myForm.validate();
            // 发送请求
            await addArticleCate(this.form);
            // 添加提示
            this.$message.success("添加成功");
        }

        if (this.dialogType === 'edit') {
            const { id, cate_name, cate_alias } = this.form
            await updateArticleCate({ id, cate_name, cate_alias})
            this.$message.success('修改成功')
        }

        // 重新渲染
        this.getCateList();
        // 关闭弹窗
        this.dialogVisible = false;
        // 初始化数据
        this.$refs.myForm.resetFields();
      } catch (e) {
        console.log(e);
      }
    },
    del (id) {
        this.id = id
        console.log(this.id)
        this.delDialog = true
    },
   async delCate () {
        console.log(this.id)
        await delArticleCate(this.id)
        this.$message.success('删除成功')
        this.delDialog = false
        this.getCateList()
    },
    async openDialog(type, id) {
        this.dialogType = type
        this.dialogVisible = true

        if (type != 'add') {
           const res = await getArticleCateInfo(id)
           this.form = {
            ...res.data
           }
        }
    }
  },
  computed: {
    dialogTitle () {
        let title = '文章分类'
        if (this.dialogType === 'add') title = '添加文章分类'
        if (this.dialogType === 'edit') title = '修改文章分类'
        return title
    }
  }
};
</script>

<style lang="less" scoped>
.box-card {
  .clearfix {
    display: flex;
    justify-content: space-between;
    align-items: center;

    span {
      font-size: 18px;
      font-weight: bold;
    }
  }

  .actions {
    display: flex;

  }
}
</style>